using System;
using System.Collections.Generic;
using System.Data;
using MapTeams.Umbraco.Polls.Dto;
using System.Linq;
namespace MapTeams.Umbraco.Polls.UI
{
    /// <summary>
    /// Helper class:
    /// 1. for creating polls datastructure from dataset.
    /// 2. for creating UI layout.
    /// 3. other tasks when needed.
    /// 
    /// Author: 
    /// Indra Kurniawan
    /// MAP Teams (http://www.mapits.com)
    /// </summary>
    public class PollsUtils
    {
        /// <summary>
        /// Build our poll data structure and calculate all percentage
        /// IsAlreadyVoted flag will be set later
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static MapTeamsPolls BuildPoll(DataSet ds)
        {
            MapTeamsPolls result = new MapTeamsPolls();
            result.PollID = (int)ds.Tables[0].Rows[0]["PollID"];
            result.Question = ds.Tables[0].Rows[0]["Question"].ToString();
            result.BlockMode = ds.Tables[0].Rows[0]["BlockMode"].ToString();


            foreach (DataRow dr in ds.Tables[1].Rows)
            {
                result.Choices.Add(new MapTeamsPollsChoice()
                {
                    PollChoiceID = (int)dr["PollChoiceID"],
                    PollID = (int)dr["PollID"],
                    Choice = (string)dr["Choice"],
                    VoteCount = (int)dr["VoteCount"],
                });
            }

            result.CalculateAllPercentage();
            return result;
        }

        /// <summary>
        /// use Mustache template engine to build html output from the MapTeamsPolls data structure
        /// </summary>
        /// <param name="template"></param>
        /// <param name="pollInfo"></param>
        /// <returns></returns>
        public static string ShowPoll(string template, MapTeamsPolls pollInfo)
        {
            string result;
            result = Nustache.Core.Render.StringToString(template, pollInfo);
            return result;
        }


        [Obsolete("use new method which implement Mustache template engine")]
        //this is old ways for creating html layout; the new one use Mustache templating engine for the server side and client side
        public static string GetResultHTML(MapTeamsPolls pollInfo)
        {
            var choices = pollInfo.Choices;
            int totalVotes = pollInfo.TotalVotes;

            System.Text.StringBuilder sbResult = new System.Text.StringBuilder();

            foreach (var choice in choices)
            {
                string alt = string.Format("{0} votes out of {1}",
                                            choice.VoteCount, totalVotes);
                string label =
                    string.Format(
                        "<label class='poll-result'>{0} ({1} votes - {2}%)</label>",
                        choice.Choice, choice.VoteCount, choice.Percentage);

                string div =
                    string.Format("<div class='poll-chart'>" +
                               " <img src='/umbraco/plugins/MapTeamsPolls/Images/red-bar.png' " +
                               "   val='{0}%' alt='{1}' title='{1}' /> </div> ",
                                choice.Percentage, alt);

                sbResult.Append(label);
                sbResult.Append(div);

            }
            string totalVote = string.Format("<div class='poll-total'>Total Votes: {0}</div>", totalVotes);
            sbResult.Append(totalVote);

            return sbResult.ToString();
        }
    }
}